<html>
<head><meta charset="utf-8"><title>Guarantees about layout of generic aggregates · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Guarantees.20about.20layout.20of.20generic.20aggregates.html">Guarantees about layout of generic aggregates</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="183847831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Guarantees%20about%20layout%20of%20generic%20aggregates/near/183847831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Guarantees.20about.20layout.20of.20generic.20aggregates.html#183847831">(Dec 19 2019 at 14:09)</a>:</h4>
<p>Given:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">S</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="p">,</span><span class="w"> </span><span class="n">PhantomData</span><span class="o">&lt;</span><span class="n">V</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>
</pre></div>


<p>is <code>for&lt;A, B, C, D&gt; transmute::&lt;&amp;S&lt;A, B, C&gt;, &amp;S&lt;A, B, D&gt;()</code> sound?</p>
<p>If not, should it be?</p>
<hr>
<p>Notice that <code>for&lt;T&gt; PhantomData&lt;T&gt;</code> is a 1-ZST and is therefore ignored for layout purposes, but it does change the actual type.</p>



<a name="183851261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Guarantees%20about%20layout%20of%20generic%20aggregates/near/183851261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Guarantees.20about.20layout.20of.20generic.20aggregates.html#183851261">(Dec 19 2019 at 14:45)</a>:</h4>
<p>I'm quite sure that transmute doesn't follow from our current guarantees for struct layout. It's not one of the special cases where we say anything other than "nothing is guaranteed, not even determinism"</p>



<a name="183851431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Guarantees%20about%20layout%20of%20generic%20aggregates/near/183851431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Guarantees.20about.20layout.20of.20generic.20aggregates.html#183851431">(Dec 19 2019 at 14:47)</a>:</h4>
<p>But clearly this kind of transmute is extremly useful for some uses of phantom types and requiring repr(C) on those aggregates has big downsides so IMO we eventually need some way to make this work while still getting e.g. automatic struct reordering for reducing padding</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>